package com.mission.game.components;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.mission.game.R;

public class CreateGPSHelper extends MapActivity {

	protected int _lat, _lon;
	protected GeoPoint _point;
	private MapView myMapView;
	private MapController myMapController;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.gps_helper);
		myMapView = (MapView) findViewById(R.id.mapview);
		myMapController = myMapView.getController();
		myMapView.setBuiltInZoomControls(true);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			if (extras.containsKey("latitude")) {
				_lat = extras.getInt("latitude");
			} else {
				_lat = 0;
			}
			if (extras.containsKey("longitude")) {
				_lon = extras.getInt("longitude");
			} else {
				_lon = 0;
			}
		}
		_point = new GeoPoint(_lat, _lon);
		myMapController.animateTo(_point);
		Drawable marker = getResources().getDrawable(
				android.R.drawable.ic_menu_myplaces);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(),
				marker.getIntrinsicHeight());
		myMapView.getOverlays().add(new InterestingLocations(marker, _point));

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public void backToTaskClicked(View v) {
		_lat = _point.getLatitudeE6();
		_lon = _point.getLongitudeE6();
		getIntent().putExtra("latitude", _lat).putExtra("longitude", _lon);
		setResult(RESULT_OK, getIntent());
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) { // Back key pressed
			getIntent().putExtra("latitude", _lat).putExtra("longitude", _lon);
			setResult(RESULT_OK, getIntent());
		}
		return super.onKeyDown(keyCode, event);
	}

	class InterestingLocations extends ItemizedOverlay<OverlayItem> {

		private List<OverlayItem> locations = new ArrayList<OverlayItem>();

		public InterestingLocations(Drawable defaultMarker, GeoPoint point) {
			super(boundCenterBottom(defaultMarker));
			// create locations of interest
			locations.add(new OverlayItem(point, "Task target", "Task target"));
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return locations.get(i);
		}

		@Override
		public int size() {
			return locations.size();
		}

		@Override
		public boolean onTap(GeoPoint p, MapView arg1) {
			CreateGPSHelper.this._point = p;
			locations.clear();
			locations.add(new OverlayItem(p, "Task target", "Task target"));
			populate();
			Toast.makeText(getBaseContext(), 
                    p.getLatitudeE6() / 1E6 + "," + 
                    p.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();
			return super.onTap(p, arg1);
		}

	}
}